const mysql = require('mysql');
const pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'localhost',
  user            : 'root',
  password        : 'root',
  database        : 'image_bed'
});
 

let query = function( sql, values ) {
  // 返回一个 Promise
  return new Promise(( resolve, reject ) => {
    pool.getConnection(function(err, connection) {
      if (err) {
        reject( err )
      } 
      else {
        try{
          connection.query(sql, values, ( err, rows) => {
            if ( err ) {
              // reject( err )
              resolve( err)
            } 
            else {
              resolve( rows )
             }
          })
        }
        catch(e){
          reject(e)
        }
        finally{
          // 结束会话
          connection.release()
        }  
      }
    })
  })
}

module.exports =  query